package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"react-blog-server/app/dao"
	"react-blog-server/app/dao/model"
	"react-blog-server/app/service"
	"react-blog-server/common/response"
)

func NewArticleHttp(group *gin.RouterGroup) {
	group.POST("/post", PostArticle)
}

// 发布文章
func PostArticle(c *gin.Context) {
	userInfo, err := service.UserService.GetLoginUserInfo(c)
	if err != nil {
		response.Fail(c, http.StatusUnauthorized, "未登录，请先登录")
		return
	}

	var article model.Article
	if err := c.BindJSON(&article); err != nil {
		response.Fail(c, http.StatusBadRequest, err.Error())
		return
	}

	article.UserId = userInfo.Id

	if err := dao.DB.Create(&article).Error; err != nil {
		response.Fail(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Ok(c)
}
